<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" width="85%" destroy-on-close @closed="$emit('closed')" draggable align-center overflow>
        <el-container>
            <el-header>
                <div class="left-panel">
                    <el-button type="primary" @click="back">确定</el-button>
                    <el-button type="primary" icon="el-icon-plus" v-auth="'goods.add'" @click="add">新增</el-button>
                </div>
                <div class="right-panel">
                    <div class="right-panel-search">
                    <el-input v-model="search.name" placeholder="请输入颜色" clearable @keyup.enter="upsearch"></el-input>
                    <el-input v-model="search.zh_name" placeholder="请输入颜色中文" clearable @keyup.enter="upsearch"></el-input>
                    <el-input v-model="search.color_code" placeholder="请输入编号" clearable @keyup.enter="upsearch"></el-input>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                    </div>
                </div>
            </el-header>
            <el-header style="height:auto">
                <div>
                    已选择颜色: 
                    <el-tag v-for="(item,index) in selection_array" :key="index" closable type="info" style="margin-right: 20px" @close="delTag(index)">
                        {{ `${item.color_code}#${item.zh_name}` }}
                    </el-tag>
                </div>
            </el-header>
            <el-main class="nopadding">
                <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" @row-dblclick="dbselectionChange" stripe remoteSort remoteFilter border height="650" :params="query">
                    <el-table-column type="selection" width="50" reserve-selection align="center" v-if="type != 1"></el-table-column>
                    <el-table-column type="index" label="" fixed align="center" width="50"></el-table-column>
                    <el-table-column label="颜色" prop="name" align="center"></el-table-column>
					<el-table-column label="中文" prop="zh_name" align="center"></el-table-column>
					<el-table-column label="编号" prop="color_code" align="center"></el-table-column>
                </scTable>
            </el-main>
        </el-container>
        <template #footer>
        <el-button @click="visible = false">取 消</el-button>
        </template>
        <save-add v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-add>
    </el-dialog>
</template>

<script>
import saveAdd from './save.vue'

export default {
    props: {
        query: {type: Object, default: () => ({})},
    },
    components: {saveAdd},
    emits: ['success', 'closed'],
    data() {
        return {
            mode: "add",
            titleMap: {
                add: '选择颜色',
                edit: '选择颜色',
                show: '查看'
            },
            visible: false,
            isSaveing: false,
            apiObj: this.$API.basic.color.list,
            selection: [],
            selection_array: [],
            search: {
                name: null,
                zh_name: null,
                color_code: null,
            },
            dialog: {
                save: false,
            },
        }
    },
    mounted() {
    },
    methods: {
        // 显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        // 表单注入数据
        setData(data) {
            console.log(data);
        },
        // 选择事件
        selectionChange(selection) {
            this.selection = selection;
            // 不再直接替换selection_array，而是根据选择状态更新
            if (selection.length > 0) {
                // 检查是否有新增的选中项
                selection.forEach(item => {
                    const exists = this.selection_array.some(selected => selected.id === item.id);
                    if (!exists) {
                        this.selection_array.push(item);
                    }
                });
            }
            // 当selection为空时，不做任何操作，保留已选择的内容
        },
        dbselectionChange(selection){
            this.$emit('success', [selection], this.type)
            this.visible = false;
        },
        // 选中返回
        back() {
            this.$emit('success', this.selection_array, this.type)
            this.visible = false;
        },
        //颜色新增
        add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('add')
            })
        },
        // 更新颜色新增
        handleSuccess(data, mode) {
            if (mode == 'add') {
                this.$refs.table.refresh()
            } else if (mode == 'edit') {
                this.$refs.table.refresh()
            }
        },
        //搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
        delTag(index){
            this.selection_array.splice(index, 1)
        }
    }
}
</script>

<style></style>
